//
//  JSPwdSeetingVC.m
//  NiubilityProject
//
//  Created by Apple on 2017/12/2.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSPwdSeetingVC.h"

@interface JSPwdSeetingVC ()

@property (weak, nonatomic) IBOutlet QMUITextField *oldPwdTF;
@property (weak, nonatomic) IBOutlet QMUITextField *NewPwdTF;
@property (weak, nonatomic) IBOutlet QMUITextField *checkPwdTF;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contenHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *marginSpace2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *marginSpace1;


@end

@implementation JSPwdSeetingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.oldPwdTF.placeholder = JSLocalizedString(@"seetingPwdOld");
    self.NewPwdTF.placeholder = JSLocalizedString(@"seetingPwdNew");
    self.checkPwdTF.placeholder = JSLocalizedString(@"seetingPwdAgin");
    [self.confirmBtn setTitle:JSLocalizedString(kLocalizableOk) forState:UIControlStateNormal];
    self.contenHeight.constant = kRealValue(50);
    self.leftSpace.constant = self.rightSpace.constant = kRealValue(15);
    self.marginSpace1.constant = self.marginSpace2.constant = kRealValue(20);
    self.extendedLayoutIncludesOpaqueBars = NO;
    JS_WeakSelf(self)
    RACSignal *enableSignal = [[RACSignal combineLatest:@[self.oldPwdTF.rac_textSignal,self.NewPwdTF.rac_textSignal,self.checkPwdTF.rac_textSignal]] map:^id(id value) {
        NSString *s1 = value[0];
        NSString *s2 = value[1];
        NSString *s3 = value[2];
        return @(s1.length && s2.length > 5 && s2.length < 12 && [s3 isEqualToString:s2]);
    }];
    self.confirmBtn.rac_command = [[RACCommand alloc] initWithEnabled:enableSignal signalBlock:^RACSignal *(id input) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBManager showLoading];
        });
        [JSNetManager js_courrentProjectRequestWithMouth:@"resetPassword" cache:NO ifShowFinishStatu:NO parameters:@{@"old_password":weakself.oldPwdTF.text,@"password":weakself.NewPwdTF.text,@"repassword":weakself.checkPwdTF.text,@"token":userManager.curUserInfo.token}.mutableCopy successBlock:^(id response) {
            if ([response[@"status"] integerValue] == 200) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [JSAlertHelper showSucceedHideAfterDelay:2 text:JSLocalizedString(kLocalizableSuccess) inView:nil animation:YES];
                     [weakself.navigationController popViewControllerAnimated:YES];
                });
               
            }else{
                 [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(kLocalizableNetworkError) inView:nil animation:YES];
            }
        } failureBlock:nil];
        return [RACSignal empty];
    }];
}

- (void)didInitialized{
    [super didInitialized];
    self.title = JSLocalizedString(@"settingPassword");
}


@end
